<?php
	$tplPro = new XTemplate('view/website/website.htm');
	
	$error 			= '';
	$tplPro->assign('baseUrl', $baseUrl);
	
	if($_POST){
		
		$banner 	= _POST('banner');
		$contact 	= _POST('contact_content');
		$yahoo 		= _POST('yahoo');
		$sky 		= _POST('sky');
		$fb 		= _POST('fb');
		$twitter	= _POST('twitter');
		if ( $banner['left'] ) {
			$bannerLeft = implode(',', $banner['left']);
			$data = array('banner_left'=>1);
			$db->update('tablbanner', $data, " banner_id IN ($bannerLeft) ");
		}
		if ( $banner['right'] ) {
			$bannerRight = implode(',', $banner['right']);
			$data = array('banner_right'=>1);
			$db->update('tablbanner', $data, " banner_id IN ($bannerRight) ");
		}
		if ( $banner['delete'] ) {
			$bannerDelete = implode(',', $banner['delete']);
			$sql = "SELECT * FROM tablbanner WHERE banner_id IN ($bannerDelete) ";
			$banners = $db->fetchAll($sql);
			foreach ( $banners as $banner ) {
				if(file_exists('..'.$banner['img'] )){
					unlink('..'.$banner['img']);
				}
			}
			$db->delete('tablbanner'," banner_id IN ($bannerDelete) ");
		}
		if($contact){
			$sql = "SELECT * FROM tblwebinfo WHERE type='contact'";
			$contactrs = $db->fetchOne($sql);
			
			if($contactrs['content']){
				$data = array('content'=>$contact);
				$db->update('tblwebinfo', $data, " type = 'contact' ");
			}else{
				
				$data = array('content'=>$contact,
							  'type'=>'contact');
				$db->insert('tblwebinfo', $data);
			}
		}
		if($yahoo){
			$sql = "SELECT * FROM tblwebinfo WHERE type='yahoo'";
			$contactrs=array();
			$contactrs = $db->fetchOne($sql);
			
			if($contactrs['content']){
				$data = array('content'=>$yahoo);
				$db->update('tblwebinfo', $data, " type = 'yahoo' ");
			}else{
				
				$data = array('content'=>$yahoo,
							  'type'=>'yahoo');
				$db->insert('tblwebinfo', $data);
			}
		}
		if($sky){
			$sql = "SELECT * FROM tblwebinfo WHERE type='sky'";
			$contactrs=array();
			$contactrs = $db->fetchOne($sql);
			
			if($contactrs['content']){
				$data = array('content'=>$sky);
				$db->update('tblwebinfo', $data, " type = 'sky' ");
			}else{
				
				$data = array('content'=>$sky,
							  'type'=>'sky');
				$db->insert('tblwebinfo', $data);
			}
		}
		if($fb){
			$sql = "SELECT * FROM tblwebinfo WHERE type='fb'";
			$contactrs=array();
			$contactrs = $db->fetchOne($sql);
			
			if($contactrs['content']){
				$data = array('content'=>$fb);
				$db->update('tblwebinfo', $data, " type = 'fb' ");
			}else{
				
				$data = array('content'=>$fb,
							  'type'=>'fb');
				$db->insert('tblwebinfo', $data);
			}
		}
		if($twitter){
			$sql = "SELECT * FROM tblwebinfo WHERE type='twitter'";
			$contactrs=array();
			$contactrs = $db->fetchOne($sql);
			
			if($contactrs['content']){
				$data = array('content'=>$twitter);
				$db->update('tblwebinfo', $data, " type = 'twitter' ");
			}else{
				
				$data = array('content'=>$twitter,
							  'type'=>'twitter');
				$db->insert('tblwebinfo', $data);
			}
		}
	}
	$sql = "SELECT * FROM tablbanner WHERE 1=1";
	$banners = $db->fetchAll($sql);
	foreach ( $banners as $banner ) {
		if( $banner['banner_left'] == 1){
			$banner['chkleft'] = 'checked="checked"';
		}
		if( $banner['banner_right'] == 1 ){
			$banner['chkright'] = 'checked="checked"';
		}
		$tplPro->insert_loop('website.list', array('list'=>$banner));
	}
	$sql = "SELECT * FROM tblwebinfo WHERE 1=1";
	$contacts = $db->fetchAll($sql);
	foreach($contacts as $item){
		if($item['type']=='contact'){
			$tplPro->assign('contact_content', $item['content']);
		}
		if($item['type']=='yahoo'){
			$tplPro->assign('yahoo', $item['content']);
		}
		if($item['type']=='sky'){
			$tplPro->assign('sky', $item['content']);
		}
		if($item['type']=='fb'){
			$tplPro->assign('fb', $item['content']);
		}
		if($item['type']=='twitter'){
			$tplPro->assign('twitter', $item['content']);
		}
		if($item['type']=='background'){
			$background = $item['content'];
		}
		if($item['type']=='counter'){
			$counter = $item['content'];
		}
	}
	if($background){
		$tplPro->assign('background', $background);
		$tplPro->parse('website.BACKGROUND');
	}else{
		$tplPro->parse('website.UPLOADBACKGROUND');
	}
	$tplPro->assign('counter', $counter ? $counter : 0);
	$tplPro->parse('website');
	$content = $tplPro->text('website');